﻿#region Creditos

/* Autor: Edilberto Sánchez Forero	*/
/* Email: esanchezfo@gmail.com		*/
/* Licencia: LGPL, Software Libre	*/
/* País: Colombia, Bogotá			*/

#endregion

#region Librerias

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Windows.Forms;
using System.IO;
using ESF.General;
using ESF.General.Entity;
using ESF.General.Text;

#endregion


namespace ESF.WinForms
{
	public static class ListLoader
	{
		#region Limpieza
		
		public static void Clear (ComboBox pList)
		{
			pList.Items.Clear ();
			pList.Enabled = false;
		}
		
		#endregion
		

		#region Combobox

		public static bool IsThereSelectionBasicReference (ComboBox pList)
		{
			return ListLoader.GetSelectedValueBasicReference (pList) != null;
		}


		public static bool IsThereSelection<O> (ComboBox pList) where O : class
		{
			return ListLoader.GetSelectedValue<O> (pList) != null;
		}


		public static bool SelectByNameBasicReference (ComboBox pList, string pText)
		{
			return SelectByNameBasicReference (pList, pText, ComparisonTypes.Equals);
		}


		public static bool SelectByNameBasicReference (ComboBox pList, string pText, ComparisonTypes pComparisonType)
		{
			return SelectByText <BasicReference> (pList, pText, "Name", pComparisonType);
		}


		public static bool SelectByCodeBasicReference (ComboBox pList, string pText)
		{
			return SelectByCodeBasicReference (pList, pText, ComparisonTypes.Equals);
		}
		

		public static bool SelectByCodeBasicReference (ComboBox pList, string pText, ComparisonTypes pComparisonType)
		{
			return SelectByText <BasicReference> (pList, pText, "Code", pComparisonType);
		}


		public static bool SelectByText<O> (ComboBox pList, string pText, string pTextMember, ComparisonTypes pComparisonType)
		{
			Type type;

			bool found;

			string tempText;


			type = typeof (O);

			for (int i = 0; i < pList.Items.Count; i++)
			{
				tempText = type.GetProperty (pTextMember).GetValue (pList.Items [i], null).ToString ();

				switch (pComparisonType)
				{
					case ComparisonTypes.LikeFull:
						found = tempText.ToUpper ().Contains (pText.ToUpper ());
						break;

					case ComparisonTypes.LikeLeft:
						found = tempText.EndsWith(pText, StringComparison.InvariantCultureIgnoreCase);
						break;

					case ComparisonTypes.LikeRight:
						found = tempText.StartsWith(pText, StringComparison.InvariantCultureIgnoreCase);
						break;

					default:
						found = tempText.Equals (pText, StringComparison.InvariantCultureIgnoreCase);
						break;
				}

				if (found)
				{
					pList.SelectedIndex = i;
					return true;
				}
			}

			return false;
		}
		
		
		public static void RemoveSelectItem (ComboBox pList)
		{
			BasicReference value;


			if (pList.Items.Count == 0) { return; }

			value = pList.Items [0] as BasicReference;

			if (value != null) 
			{ 
				if (string.IsNullOrEmpty (value.Code))
				{
					pList.Items.RemoveAt (0);
				}
			}

			if (pList.Items.Count > 0)
			{
				pList.SelectedIndex = 0;
			}
		}


		public static bool ChooseSelectItem (ComboBox pList)
		{
			BasicReference item;

			
			if (pList.Items.Count == 0) { return false; }

			item = pList.Items [0] as BasicReference;

			if (string.IsNullOrEmpty (item.Code))
			{
				pList.SelectedIndex = 0;
				return true;
			}

			return false;
		}


		public static bool IsSelectedValueINBasicReference (ComboBox pList, params string [] pValues)
		{
			BasicReference codeValue;


			if (pValues == null || pValues.Length == 0 || pList.Items.Count == 0) { return false; }

			codeValue = GetSelectedValueBasicReference (pList);

			if (codeValue == null)
			{
				return false;
			}

			foreach (string value in pValues)
			{
				if (codeValue.Code == value) { return true; }
			}

			return false;
		}


		public static bool GetSelectedValueBasicReference (ComboBox pList, out BasicReference pSelectedValue)
		{
			pSelectedValue = GetSelectedValueBasicReference (pList);

			return (pSelectedValue != null);
		}


		public static BasicReference GetSelectedValueBasicReference (ComboBox pList)
		{
			BasicReference item;


			item = GetSelectedValue <BasicReference> (pList);

			if (item == null || string.IsNullOrEmpty (item.Code)) { return null; }

			return item;
		}

		
		public static O GetSelectedValue<O> (ComboBox pList) where O : class
		{
			if (pList.Items.Count == 0) { return null; }
			
			return pList.SelectedItem as O;
		}
		
		
		public static void AddItem (ComboBox pList, string pItemValueKey, string pItemText)
		{
			BasicReference item;
			
			
			item = new BasicReference () { Code = pItemValueKey, Name = pItemText };
			
			
			if (!pList.Items.Contains (item))
			{
				if (string.IsNullOrEmpty (pItemValueKey))
				{
					pList.Items.Insert (0, new BasicReference () { Code = string.Empty, Name = TextHelper.GeneralSelectItemText });
				}
				else
				{
					pList.Items.Add (item);
				}
			}
		}
		
		
		public static void AddSelectItem (ComboBox pList)
		{
			AddSelectItem (pList, TextHelper.GeneralSelectItemText);
		}
		
		
		public static void AddSelectItem (ComboBox pList, string pSelectItemText)
		{
			if (!SelectByCodeBasicReference (pList, TextHelper.SelectOneValue))
			{
				pList.Items.Insert (0, new BasicReference () { Name = pSelectItemText, Code = TextHelper.SelectOneValue });
			}
		}
		
		
		public static bool LoadXml (ComboBox pList, string pTableXml, string pValueName, string pTextName) 
		{
			DataSet dtsDatos;
		
		
			Clear (pList);
			
			if (String.IsNullOrEmpty (pTableXml)) { return false; }

			dtsDatos = new DataSet ();
			
			dtsDatos.ReadXml (new StringReader (pTableXml));
						
			return LoadTable (pList, dtsDatos.Tables [0]);
		}


		public static bool LoadListTexts (ComboBox pList, IList<string> pTexts)
		{
			return LoadListTexts (pList, pTexts, false);
		}


		public static bool LoadListTexts (ComboBox pList, IList<string> pTexts, bool pWithIndexValue)
		{
			Clear (pList);

			if (pTexts == null || pTexts.Count == 0) { return false; }

			return LoadListTexts (pList, pTexts.ToArray (), pWithIndexValue);
		}


		public static bool LoadListTexts (ComboBox pList, string [] pTexts, bool pWithIndexValue)
		{
			List<BasicReference> items;


			Clear (pList);

			if (pTexts == null || pTexts.Length == 0) { return false; }

			items = new List<BasicReference> (pTexts.Length);

			if (pWithIndexValue)
			{
				for (int i = 0; i < pTexts.Count (); i++)
				{
					items.Add (new BasicReference () { Code = i.ToString (), Name = pTexts [i] });
				}
			}
			else
			{			
				foreach (string text in pTexts)
				{
					items.Add (new BasicReference () { Code = text, Name = text });
				}
			}

			return LoadList (pList, items);
		}
		
		
		public static bool LoadList<L> (ComboBox pList, IList<BasicReference> pListItems)
		{
			return LoadList<L, BasicReference> (pList, pListItems, "ID", "Name");
		}
		
		
		public static bool LoadList<L, T> (ComboBox pList, IList<T> pListItems, string pValueName, string pTextName)
		{
			Type type;
			
		
			Clear (pList);
			
			if (pListItems == null || pListItems.Count == 0) { return false; }
			
			type = pListItems [0].GetType ();
			
			pList.ValueMember = pValueName;
			pList.DisplayMember = pTextName;
			
			ListLoader.AddSelectItem (pList);
			
			for (int i = 0; i < pListItems.Count; i++)
			{
				pList.Items.Add (pListItems [i]);
			}
			
			pList.SelectedIndex = 0;

			pList.Enabled = true;
			
			return true;
		}
		

		public static bool LoadTable (ComboBox pList, DataTable pTable)
		{
			if (pTable == null || pTable.Rows.Count == 0) { return false; }
			
			return LoadTable (pList, pTable, pTable.Columns [0].ColumnName, pTable.Columns [1].ColumnName);
		}
		
		
		public static bool LoadTable (ComboBox pList, DataTable pTable, string pValueName, string pTextName)
		{
			Clear (pList);

			if (pTable == null || pTable.Rows.Count == 0) { return false; }

			pList.ValueMember = "Code";
			pList.DisplayMember = "Name";
			
			pList.Items.Add (new BasicReference () { Code = string.Empty, Name = TextHelper.GeneralSelectItemText });
			
			foreach (DataRow dtr in pTable.Rows)
			{
				pList.Items.Add (new BasicReference () { Code = dtr [pValueName].ToString (), Name = dtr [pTextName].ToString () });
			}
			
			pList.SelectedIndex = 0;
			
			return true;
		}


		public static void LoadList (ComboBox pList, Type pEnumType)
		{
			List<BasicReference> items;


			items = new List<BasicReference> ();

			foreach (string enumItem in Enum.GetNames (pEnumType))
			{
				items.Add (new BasicReference () { Code = enumItem, Name = enumItem });
			}

			LoadList (pList, items);
		}
		
		
		public static bool LoadList (ComboBox pList, params string [] pKeyValueItems)
		{
			Clear (pList);
		
			if (pKeyValueItems == null || pKeyValueItems.Length == 0)
			{
				return false;
			}
			
			pList.ValueMember = "Code";
			pList.DisplayMember = "Name";
			
			pList.Items.Add (new BasicReference () { Code = string.Empty, Name = TextHelper.GeneralSelectItemText });
			
			for (int i = 0; i < pKeyValueItems.Length; i+=2)
			{
				pList.Items.Add (new BasicReference () { Code = pKeyValueItems [i + 1], Name = pKeyValueItems [i] });
			}
			
			pList.SelectedIndex = 0;
			pList.Enabled = true;

			return true;
		}
		
		
		public static bool LoadList (ComboBox pList, List<BasicReference> pItems)
		{
			Clear (pList);
		
			if (pItems == null || pItems.Count == 0)
			{
				return false;
			}
			
			pList.ValueMember = "Code";
			pList.DisplayMember = "Name";
			
			foreach (BasicReference item in pItems)
			{
				pList.Items.Add (item);
			}
			
			AddSelectItem (pList);
			
			pList.SelectedIndex = 0;
			pList.Enabled = true;

			return true;
		}

		#endregion


		#region ListBox

		public static void AddItem(ListBox pList, string pItemValueKey, string pItemText)
		{
			BasicReference item;


			item = new BasicReference() { Code = pItemValueKey, Name = pItemText };


			if (!pList.Items.Contains(item))
			{
				pList.Items.Add(item);
			}
		}


		public static void RemoveAll (ListBox pList)
		{
			pList.Items.Clear ();
		}


		public static void RemoveSelected(ListBox pList)
		{
			int adjust = 0;


			foreach (int item in pList.SelectedIndices)
			{
				pList.Items.RemoveAt (item - adjust);

				adjust ++;
			}
		}


		public static IEnumerable<BasicReference> GetSelectedBasicReferences(ListBox pList)
		{
			foreach (BasicReference item in pList.Items)
			{
				yield return item;
			}
		}


		public static IEnumerable<string> GetSelectedBasicReferenceCodes (ListBox pList)
		{
			foreach (BasicReference item in pList.Items)
			{
				yield return item.Code;
			}
		}


		public static IEnumerable<string> GetSelectedBasicReferenceNames(ListBox pList)
		{
			foreach (BasicReference item in pList.Items)
			{
				yield return item.Name;
			}
		}


		public static IEnumerable<int?> GetSelectedBasicReferenceIDs(ListBox pList)
		{
			foreach (BasicReference item in pList.Items)
			{
				yield return item.ID;
			}
		}


		/*public static void RemoveSelectItem<L, O> (L pList) where L : ComboBox
		{
			RemoveByValue<L, O> (pList, string.Empty);
		}
		
		
		public static void RemoveByValue<L, O> (L pList, params string [] pValues) where L : ComboBox
		{
			O item;


			if (pValues == null || pValues.Length == 0) { return; }

			foreach (string val in pValues)
			{
				item = pList.Items [.FindByValue(val.Trim ());

				if (item != null)
				{
					pList.Items.Remove (item);
				}
			}
		}*/
		
		#endregion		
	}
}
